﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more.
//
// Copyright (c) 2009 Niklas Kroll
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.Threading;

namespace wpControl1
{
    static class Program
    {
        internal static ImageManager ImageManager = new ImageManager();
        internal static ZoomStyleManager ZoomStyleManager = new ZoomStyleManager();
        internal static ConditionManager ConditionManager = new ConditionManager();
        internal static CfgFileManager CfgFileManager = new CfgFileManager();
        internal static WallpaperManager WallpaperManager = new WallpaperManager();
        internal static Wallpaper Wallpaper = new Wallpaper();
        internal static ChangeClient ChangeClient = new ChangeClient();
        internal static SettingsManager SettingsManager = new SettingsManager();
        internal static ConfigManager ConfigManager = new ConfigManager();
        internal static StartManager StartManager = new StartManager();
        internal static ResourceManager LanguageManager = new ResourceManager("wpControl1.Language", Assembly.GetExecutingAssembly());
        internal static TrayApp TrayApp = new TrayApp();
        internal static ControlPanel ControlPanel;

        /// <summary>
        /// Main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
            // Init lists
            Program.Initialize();
            // Load settings from file
            Program.CfgFileManager.LoadSettingsFile();
            // Open ControlPanel if requested
            if (Program.SettingsManager.StartWithControlPanel)
            {
                Program.EnterControlPanel();
            }
            // Load saved images from file
            Program.CfgFileManager.LoadImageListFile();
            // Load saved condition from file
            Program.CfgFileManager.LoadConditionFile();
            // Update ControlPanel to loaded configuration
            if (Program.SettingsManager.StartWithControlPanel)
            {
                Program.ControlPanel.RestoreValues();
            }
            // Enter the TrayApp
            Program.TrayApp.Enter();
            // Start message loop (no form - ControlPanel is optional)
            Application.Run();
        }

        internal static void Initialize()
        {
            // Create new image list
            Program.ImageManager.InitImageStack();
            // Create new style list
            Program.ZoomStyleManager.InitStyleStack();
        }
        
        /// <summary>
        /// Enters the TrayApp. In this mode WallpaperControl runs only in SysTray.
        /// </summary>
        /// <remarks>Currently just use TrayApp.Enter()</remarks>
        internal static void EnterTrayApp()
        {
        }

        /// <summary>
        /// Enters the ControlPanel. This mode allows users to configure WallpaperControl.
        /// </summary>
        internal static void EnterControlPanel()
        {
            Program.ControlPanel = new ControlPanel();
            Program.ControlPanel.Show();
        }

        /// <summary>
        /// Exits the TrayApp.
        /// </summary>
        internal static void ExitTrayApp()
        {
        }

        /// <summary>
        /// Exits the ControlPanel.
        /// </summary>
        internal static void ExitControlPanel()
        {
            Program.ControlPanel.Close();
        }
    }
}
